home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / GSRC208A.ZIP / IOTOP.C < prev    next >
C/C++ Source or Header  |  1993-08-26  |  17KB  |  642 lines

  1. #include "copyleft.h"
  2.  
  3. /*
  4.     GEPASI - a simulator of metabolic pathways and other dynamical systems
  5.     Copyright (C) 1989, 1992, 1993  Pedro Mendes
  6. */
  7.  
  8. /*************************************/
  9. /*                                   */
  10. /*        MS-WINDOWS front end       */
  11. /*                                   */
  12. /*         Topology files I/O        */
  13. /*                                   */
  14. /*          QuickC/WIN 1.0           */
  15. /*                                   */
  16. /*   (include here compilers that    */
  17. /*   compiled GWSIM successfully)    */
  18. /*                                   */
  19. /*************************************/
  20.  
  21.  
  22. /*
  23.   this file is used both by GWSIM and GWTOP and GWTOP
  24.   must define the symbol GWTOP, otherwise, this file
  25.   will generate stuff for GWSIM
  26. */
  27.  
  28. #include <windows.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <io.h>
  33. #include <sys\types.h>
  34. #include <sys\stat.h>
  35. #include "globals.h"
  36. #include "iotop.h"
  37. #include "strtbl.h"                        /* symbols for the string table            */
  38. #ifdef GWTOP
  39. #include "gwtop.h"
  40. #include "topgvar.h"
  41. #include "gep1.h"
  42. #else
  43. #include "gwsim.h"
  44. #include "simgvar.h"
  45. #include "gep2.h"
  46. #endif
  47.  
  48. #pragma alloc_text( CODE12, TreeToBuf, BufToTree, TopToBuf, WriteTop, BufToTop, ReadTop )
  49.  
  50. /* write a rate equation tree to a buffer                            */
  51.  
  52. void TreeToBuf( int i, LPSTR Buff )
  53. {
  54.  char a1[128];
  55.  int j;
  56.  
  57.  wsprintf( (LPSTR) Buff, "\f%s\n", (LPSTR) tree[i].descr );
  58.  wsprintf( (LPSTR) a1, "%d %d %d %d %d %d %d %d\n", tree[i].nnode,
  59.                                                        tree[i].nid,
  60.                                                     tree[i].nnum,
  61.                                                     tree[i].nconst,
  62.                                                     tree[i].nsub,
  63.                                                     tree[i].npro,
  64.                                                     tree[i].nmodf,
  65.                                                     tree[i].revers );
  66.  lstrcat( Buff, (LPSTR) a1 );
  67.  for( j=0; j<tree[i].nnode; j++ )
  68.  {
  69.   wsprintf( (LPSTR) a1, "%c %d %d %d,", tree[i].node[j].item, (int) tree[i].node[j].val,
  70.                                   (int) tree[i].node[j].left, (int) tree[i].node[j].right );
  71.   lstrcat( Buff , (LPSTR) a1 );
  72.  }
  73.  lstrcat( Buff, (LPSTR) "\n" );
  74.  for( j=0; j<tree[i].nid; j++ )
  75.  {
  76.   wsprintf( (LPSTR) a1, "%d %s\n", (int) tree[i].id[j][9], (LPSTR) tree[i].id[j] );
  77.   lstrcat( Buff, (LPSTR) a1 );
  78.  }
  79.  for( j=0; j<tree[i].nnum; j++ )
  80.  {
  81.   gcvt( tree[i].constant[j], 8, a1 );
  82.   lstrcat( Buff, (LPSTR) a1 );
  83.   lstrcat( Buff, (LPSTR) "\n" );
  84.  }
  85. }
  86.  
  87.  
  88. char *BufToTree( char *Buff )
  89. {
  90.  int i;
  91.  char *paux;
  92.  int daux, daux1, daux2;
  93.  char caux[10];
  94.  float faux;
  95.  
  96.  /* copy the title from the buffer                                */
  97.  paux = strchr( Buff, '\n' );
  98.  if( paux == NULL )
  99.   return (char *) NULL;
  100.  /* take care of CR if one existed before LF                        */
  101.  if( *(paux-1)=='\r' ) *(paux-1) = '\0';
  102.  *paux = '\0';
  103.  if( strlen( Buff ) > 63 ) Buff[63] = '\0';
  104.  lstrcpy( (LPSTR) tr.descr, (LPSTR) Buff );
  105.  Buff = paux+1;
  106.  
  107.  /* read nnode from the buffer                                        */
  108.  if ( sscanf( Buff, "%d", &daux ) < 1 )
  109.   return (char *) NULL;
  110.  tr.nnode = daux;
  111.  
  112.  Buff = strchr( Buff, ' ' );
  113.  /* read nid from the buffer                                        */
  114.  if( (Buff == NULL ) || ( sscanf( ++Buff, "%d", &daux ) < 1 ) )
  115.   return (char *) NULL;
  116.  tr.nid = daux;
  117.  
  118.  Buff = strchr( Buff, ' ' );
  119.  /* read nnum from the buffer                                        */
  120.  if( (Buff == NULL ) || ( sscanf( ++Buff, "%d", &daux ) < 1 ) )
  121.   return (char *) NULL;
  122.  tr.nnum = daux;
  123.  
  124.  Buff = strchr( Buff, ' ' );
  125.  /* read nconst from the buffer                                    */
  126.  if( (Buff == NULL ) || ( sscanf( ++Buff, "%d", &daux ) < 1 ) )
  127.   return (char *) NULL;
  128.  tr.nconst = daux;
  129.  
  130.  Buff = strchr( Buff, ' ' );
  131.  /* read nsub from the buffer                                        */
  132.  if( (Buff == NULL ) || ( sscanf( ++Buff, "%d", &daux ) < 1 ) )
  133.   return (char *) NULL;
  134.  tr.nsub = daux;
  135.  
  136.  Buff = strchr( Buff, ' ' );
  137.  /* read npro from the buffer                                        */
  138.  if( (Buff == NULL ) || ( sscanf( ++Buff, "%d", &daux ) < 1 ) )
  139.   return (char *) NULL;
  140.  tr.npro = daux;
  141.  
  142.  Buff = strchr( Buff, ' ' );
  143.  /* read nmodf from the buffer                                    */
  144.  if( (Buff == NULL ) || ( sscanf( ++Buff, "%d", &daux ) < 1 ) )
  145.   return (char *) NULL;
  146.  tr.nmodf = daux;
  147.  
  148.  Buff = strchr( Buff, ' ' );
  149.  /* read revers from the buffer                                    */
  150.  if( (Buff == NULL ) || ( sscanf( ++Buff, "%d", &daux ) < 1 ) )
  151.   return (char *) NULL;
  152.  tr.revers = daux;
  153.  
  154.  Buff = strchr( Buff, '\n' );
  155.  if( Buff==NULL ) return (char *) NULL;
  156.  Buff++;
  157.  
  158.  for( i=0; i<tr.nnode; i++ )
  159.  {
  160.   if( ( sscanf( Buff, "%c %d %d %d", &caux[0],
  161.                                      &daux,
  162.                                      &daux1,
  163.                                      &daux2 ) < 4 ) )
  164.    return (char *) NULL;
  165.   tr.node[i].item  = caux[0];
  166.   tr.node[i].val   = (unsigned char) daux;
  167.   tr.node[i].left  = (unsigned char) daux1;
  168.   tr.node[i].right = (unsigned char) daux2;
  169.   Buff = strchr( Buff, ',' );
  170.   if( Buff==NULL ) return (char *) NULL;
  171.   Buff++;
  172.  }
  173.  for( i=0; i<tr.nid; i++ )
  174.  {
  175.   Buff = strchr( Buff, '\n' );
  176.   if( Buff==NULL ) return (char *) NULL;
  177.   Buff++;
  178.   if( sscanf( Buff, "%d %8s", &daux, caux ) < 2 )
  179.    return (char *) NULL;
  180.   lstrcpy( tr.id[i], (LPSTR) caux );
  181.   tr.id[i][8] = '\0';
  182.   tr.id[i][9] = (char) daux;
  183.  }
  184.  for( i=0; i<tr.nnum; i++ )
  185.  {
  186.   Buff = strchr( Buff, '\n' );
  187.   if( Buff==NULL ) return (char *) NULL;
  188.   Buff++;
  189.   if( sscanf( Buff, "%g", &faux ) < 1 )
  190.    return (char *) NULL;
  191.   tr.constant[i] = faux;
  192.  }
  193. /*Buff = strchr( Buff, '\n' );*/
  194.  return Buff;
  195. }
  196.  
  197.  
  198. void TopToBuf( LPSTR Buff )
  199. {
  200.  int i, j;
  201.  char auxstr[260];
  202.  
  203.  /* first line: version number                                            */
  204.  wsprintf( (LPSTR) Buff, FILE_VERSION );
  205.  /* topology's title in one line                                        */
  206.  wsprintf( (LPSTR) auxstr, "%s\n", (LPSTR) topname );
  207.  lstrcat( Buff, (LPSTR) auxstr);
  208.  /* number of steps and metabolites    in one line                            */
  209.  wsprintf( (LPSTR) auxstr, "%d %d\n", nsteps, totmet );
  210.  lstrcat( Buff, (LPSTR) auxstr);
  211.  /* stoicheiometry matrix                                                */
  212.  for(i=0;i<totmet;i++)
  213.  {
  214.   for(j=0;j<nsteps;j++)
  215.   {
  216. #ifdef GWTOP
  217.    wsprintf( (LPSTR) auxstr, "%2d ", stoiu[i*MAX_MET + j] );
  218. #else
  219.    wsprintf( (LPSTR) auxstr, "%2d ", stoi[i*MAX_MET + j] );
  220. #endif
  221.    lstrcat( Buff, (LPSTR) auxstr);
  222.   }
  223.   lstrcat( Buff, (LPSTR) "\n" );
  224.  }
  225.  /* kinetic types, reversability status and step names, one per line    */
  226.  for(i=0;i<nsteps;i++)
  227.  {
  228.   wsprintf( (LPSTR) auxstr, "%2d %2d %s\n", kinetu[i],
  229.                                               revers[i],
  230.                                               (LPSTR) stepname[i] );
  231.   lstrcat( Buff, (LPSTR) auxstr);
  232.  }
  233.  /* rstr matrix                                                            */
  234.  for(i=0;i<nsteps;i++)
  235.  {
  236.   for(j=0;j<MAX_MOL;j++)
  237.   {
  238.    wsprintf( (LPSTR) auxstr, "%2d ", (*rstr)[i][j] );
  239.    lstrcat( Buff, (LPSTR) auxstr);
  240.   }
  241.   lstrcat( Buff, (LPSTR) "\n" );
  242.  }
  243.  /* loop matrix                                                            */
  244.  for(i=0;i<nsteps;i++)
  245.  {
  246.   for(j=0;j<totmet;j++)
  247.   {
  248.    wsprintf( (LPSTR) auxstr, "%2d ", (*loop)[i][j] );
  249.    lstrcat( Buff, (LPSTR) auxstr);
  250.   }
  251.   lstrcat( Buff, (LPSTR) "\n" );
  252.  }
  253.  /* metabolite status and names, one per line                                        */
  254.  for(i=0;i<totmet;i++)
  255.  {
  256.   wsprintf( (LPSTR) auxstr, "%d %s\n", intmet[i], (LPSTR) metname[i] );
  257.   lstrcat( Buff, (LPSTR) auxstr );
  258.  }
  259. }
  260.  
  261. int WriteTop( LPSTR FName )
  262. {
  263.  GLOBALHANDLE hBuff;
  264.  HCURSOR hSaveCursor;
  265.  LPSTR Buff;
  266.  int ch1, i, udt;
  267.  WORD bufsize;
  268.  OFSTRUCT OfStruct;
  269.  
  270.  /* display the wait cursor                                            */
  271.  hSaveCursor = SetCursor(hHourGlass);
  272.  
  273.  /* set the maximum buffer size for the topology w/o user-def kin        */
  274.  bufsize = (WORD) (20 + 256 + 2 + 5 + 2 +
  275.                    totmet*(nsteps*3 + 2) +
  276.                    nsteps*(5 + NAME_L + 2) +
  277.                    nsteps*(MAX_MOL*4 + 2) +
  278.                    nsteps*(totmet*4 + 2) +
  279.                    totmet*(NAME_L + 2 + 2)
  280.                   );
  281.  hBuff = GlobalAlloc( GMEM_MOVEABLE | GMEM_ZEROINIT, bufsize );
  282.  Buff = (LPSTR) GlobalLock( hBuff );
  283.  /* write the information on the buffer                                    */
  284.  TopToBuf( Buff );
  285.  /* open the file                                                        */
  286.  if( (ch1 =